package com.scu;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;

public class beidou {
    public static void main(String args[] )
    {
        JFrame jfr=new JFrame();
        jfr.setTitle("BEIDOU");
        GridBagLayout gbl=new GridBagLayout();
        GridBagConstraints gbc=new GridBagConstraints();
        jfr.setLayout(gbl);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gbc.fill=GridBagConstraints.BOTH;

        JLabel title=new JLabel("报文解析平台");
        Add_Component(jfr,gbl,title,gbc,0,0,1,1,0,0);

        JLabel sendsetting =new JLabel("发送窗口设置");
        Add_Component(jfr,gbl,sendsetting,gbc,7,1,1,1,0,0);

        JLabel timesetting =new JLabel("接收持续时间设置");
        Add_Component(jfr,gbl,timesetting,gbc,0,2,1,1,0,0);


        JToggleButton toggleButton = new JToggleButton("开关");

        Add_Component(jfr,gbl,toggleButton,gbc,1,2,1,1,0,0);

        JLabel localAddress =new JLabel("本机主机地址 ");
        Add_Component(jfr,gbl,localAddress,gbc,7,2,1,1,0,0);

        JTextArea localAddressText =new JTextArea();
        Add_Component(jfr,gbl,localAddressText,gbc,8,2,1,1,0,0);

        JLabel localPort =new JLabel("本机端口 ");
        Add_Component(jfr,gbl,localPort,gbc,9,2,1,1,0,0);

        JTextArea localLocalText =new JTextArea();
        Add_Component(jfr,gbl,localLocalText,gbc,10,2,1,1,0,0);


        //
        JButton paraSetting=new JButton("参数解析");//主页按钮，按下后返回播放器主页
        Add_Component(jfr,gbl,paraSetting,gbc,0,3,1,1,0,0);


        JLabel timeOnOffRatio =new JLabel("  时间通断比  ");
        Add_Component(jfr,gbl,timeOnOffRatio,gbc,1,3,1,1,0,0);

        JTextArea timeOnOffRatioText =new JTextArea();
        Add_Component(jfr,gbl,timeOnOffRatioText,gbc,2,3,1,1,1,0);

        JLabel timeOnOff =new JLabel(" % ");
        Add_Component(jfr,gbl,timeOnOff,gbc,3,3,1,1,0,0);


        JTextArea timeSecond =new JTextArea();
        Add_Component(jfr,gbl,timeSecond,gbc,4,3,1,1,1,0);

        JLabel second =new JLabel(" 秒         ");
        Add_Component(jfr,gbl,second,gbc,5,3,1,1,0,0);

        JLabel msgreception =new JLabel("     报文接收    ");
        Add_Component(jfr,gbl,msgreception,gbc,6,3,1,1,0,0);

        JLabel remoteAddress =new JLabel("远端主机地址 ");
        Add_Component(jfr,gbl,remoteAddress,gbc,7,3,1,1,0,0);

        JTextArea remoteAddressText =new JTextArea();
        Add_Component(jfr,gbl,remoteAddressText,gbc,8,3,1,1,1,0);

        JLabel remotePort =new JLabel("远端端口 ");
        Add_Component(jfr,gbl,remotePort,gbc,9,3,1,1,0,0);

        JTextArea remoteLocalText =new JTextArea();
        Add_Component(jfr,gbl,remoteLocalText,gbc,10,3,1,1,1,0);



        JLabel beidouMsg =new JLabel("  北斗报文 ");
        Add_Component(jfr,gbl,beidouMsg,gbc,5,4,1,1,0,0);


        JLabel sendMsg =new JLabel("  发送消息  ");
        Add_Component(jfr,gbl,sendMsg,gbc,5,5,1,1,0,0);


        JTextArea sendMsgTxt =new JTextArea();
        Add_Component(jfr,gbl,sendMsgTxt,gbc,6,5,4,1,1,0);


        JLabel floatStatus =new JLabel("浮标状态");
        Add_Component(jfr,gbl,floatStatus,gbc,7,5,1,1,0,0);

        JTextArea floatStatusText =new JTextArea();
        Add_Component(jfr,gbl,floatStatusText,gbc,8,5,1,1,1,0);

        JLabel floatratio =new JLabel("浮标通断比");
        Add_Component(jfr,gbl,floatratio,gbc,9,5,1,1,0,0);

        JTextArea floatratioText =new JTextArea();
        Add_Component(jfr,gbl,floatratioText,gbc,10,5,1,1,1,0);

        JLabel floatratiotip =new JLabel(" % ");
        Add_Component(jfr,gbl,floatratiotip,gbc,11,5,1,1,0,0);








        JLabel communicateType =new JLabel("通信手段");
        Add_Component(jfr,gbl,communicateType,gbc,7,6,1,1,0,0);

        JTextArea communicateTypeText =new JTextArea();
        Add_Component(jfr,gbl,communicateTypeText,gbc,8,6,1,1,1,0);

        JLabel successrate =new JLabel("通信成功率");
        Add_Component(jfr,gbl,successrate,gbc,9,6,1,1,0,0);

        JTextArea successrateText =new JTextArea();
        Add_Component(jfr,gbl,successrateText,gbc,10,6,1,1,1,0);

        JLabel successratetip =new JLabel(" % ");
        Add_Component(jfr,gbl,successratetip,gbc,11,6,1,1,0,0);




        JLabel SateliteType =new JLabel("卫星类型");
        Add_Component(jfr,gbl,SateliteType,gbc,7,7,1,1,0,0);

        JTextArea SateliteTypeText =new JTextArea();
        Add_Component(jfr,gbl,SateliteTypeText,gbc,8,7,1,1,1,0);

        JLabel errorrate =new JLabel("通信误码率");
        Add_Component(jfr,gbl,errorrate,gbc,9,7,1,1,0,0);

        JTextArea errorrateText =new JTextArea();
        Add_Component(jfr,gbl,errorrateText,gbc,10,7,1,1,1,0);

        JLabel errorratetip =new JLabel(" % ");
        Add_Component(jfr,gbl,errorratetip,gbc,11,7,1,1,0,0);



        JLabel communicateRate =new JLabel("通信速率");
        Add_Component(jfr,gbl,communicateRate,gbc,7,8,1,1,0,0);

        JTextArea communicateRateText =new JTextArea();
        Add_Component(jfr,gbl,communicateRateText,gbc,8,8,1,1,1,0);

        JLabel ratetip =new JLabel("bps");
        Add_Component(jfr,gbl,ratetip,gbc,9,8,1,1,0,0);



        JButton clearButton=new JButton("清除显示");
        Add_Component(jfr,gbl,clearButton,gbc,6,10,1,1,0,0);


        JLabel recStatus =new JLabel("接收状态栏");
        Add_Component(jfr,gbl,recStatus,gbc,8,10,1,1,0,0);






        JLabel recMsg =new JLabel("  接收消息  ");
        Add_Component(jfr,gbl,recMsg,gbc,5,11,1,1,0,0);

        JTextArea recMsgTxt =new JTextArea();
        Add_Component(jfr,gbl,recMsgTxt,gbc,6,11,4,1,1,1);


        JTextArea recStatusText =new JTextArea();
        Add_Component(jfr,gbl,recStatusText,gbc,8,11,4,1,1,0);




        JButton exitButton=new JButton("退出");
        Add_Component(jfr,gbl,exitButton,gbc,2,15,1,1,0,0);

        JLabel delay =new JLabel("  时延");
        Add_Component(jfr,gbl,delay,gbc,6,15,1,1,0,0);


        JTextArea delayText =new JTextArea();
        Add_Component(jfr,gbl,delayText,gbc,7,15,1,1,0,0);

        JLabel delaySecond =new JLabel(" 秒");
        Add_Component(jfr,gbl,delaySecond,gbc,8,15,1,1,0,0);



        jfr.setSize(1200,600);//设置窗口的大小
        jfr.setVisible(true);//设置窗口为可见

        //	new UdpClient();


        //   Thread server= new Thread(new server());
        Thread client= new Thread(new client());
        //   server.start();
	    /*     try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
        client.start();

        System.out.println("server==================");


    }

    public static void Add_Component(JFrame jfr,GridBagLayout gbl,Component comp,GridBagConstraints gbc,int gridx,int gridy,int gridheight,int gridwidth,int weight_x,int weight_y)
    {
        gbc.weightx=weight_x;
        gbc.weighty=weight_y;
        gbc.gridheight=gridheight;
        gbc.gridwidth=gridwidth;
        gbc.gridx=gridx;
        gbc.gridy=gridy;
        gbl.setConstraints(comp, gbc);
        jfr.add(comp);
    }






}



class client implements Runnable{

    @Override
    public void run() {
        try(DatagramSocket socket=new DatagramSocket(9000)){
            socket.setSoTimeout(10000);
            InetAddress address=InetAddress.getByName("localhost");
            String sendstr = "hello";
            byte[] buf = sendstr.getBytes();
            DatagramPacket request=new DatagramPacket(buf,buf.length,address,9996);
            DatagramPacket response=new DatagramPacket(new byte[1024],1024);
            socket.send(request);
            socket.receive(response);
            String result=new String(response.getData(),0,response.getLength(),"US-ASCII");
            System.out.println(result);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}




class server implements Runnable{

    @Override
    public void run() {
        try(DatagramSocket socket=new DatagramSocket(8999)){
            while(true){
                DatagramPacket request=new DatagramPacket(new byte[1024],1024);
                socket.receive(request);
                String daytime=new Date().toString();
                System.out.println("rec==================" +daytime);
                byte[] data=daytime.getBytes("US-ASCII");
                DatagramPacket datagramPacket=new DatagramPacket(data,data.length,request.getAddress(),request.getPort());
                socket.send(datagramPacket);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}




